{include file="public/header296" /}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list layui-count" data-type="insert">添加</button>
        <button class="layui-btn layuiadmin-btn-list layui-count" data-type="article">案例管理</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="buttonTpl">
        {{#  if(d.status == 1){ }}
        <button class="layui-btn layui-btn-xs">显示</button>
        {{#  } else { }}
        <button class="layui-btn layui-btn-primary layui-btn-xs">隐藏</button>
        {{#  } }}
      </script>
      <script type="text/html" id="table-content-list">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>
<script>
  layui.use(['layer', 'table', 'form'], function () {
    var $ = layui.$
            , table = layui.table
            , form = layui.form;
    table.render({
      elem: '#LAY-app-content-list'
      ,url: "{:url('cases/getCategoryList')}"
      , request: {
        pageName: 'page',
        limitName: 'page_size'
      }
      , page: {
        layout: ['limit', 'count', 'prev', 'page', 'next']
        , first: '首页'
        , last: '尾页'
        , limit: 10
      }
      , cols: [[
        {field: 'id',type: 'checkbox'}
        , {field: 'id', title: 'ID'}
        ,{field: 'name', minWidth: 400, title: '分类名称',}
        ,{field: 'sort', title: '排序', align:'center', sort: true}
        ,{field: 'update_time', title: '最后更新', width:160, sort: true}
        ,{field: 'status', title: '状态', templet: '#buttonTpl', minWidth: 80}
        ,{title: '操作', minWidth: 200, fixed: 'right', toolbar: '#table-content-list'}
      ]]
      , text: {
        none: '暂无相关数据'
      }
    });

    // 监听工具条
    table.on('tool(LAY-app-content-list)', function(obj){
      var data = obj.data;
      var category_id = data.id;
      if(obj.event === 'delete'){
        layer.confirm('确定删除该分类吗？', {title : '温馨提示'}, function(index){
          $.ajax({
            url:"{:url('cases/categoryDelete')}",
            type:'post',
            data:{'id' : category_id},
            success: function(res){
              if(res.code == 0){
                obj.del();
                layer.msg(res.msg,{icon:1});
              } else{
                layer.msg(res.msg,{icon:5});
              }
            }
          });
        });
      } else if(obj.event === 'edit'){
        layer.open({
          type: 2
          ,title: '编辑分类'
          ,content: "{:url('cases/categoryEdit')}?id=" + data.id
          ,maxmin: true
          ,area: ['400px', '400px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-edit");
            iframeWindow.layui.form.on('submit(Lay-app-form-edit)', function(data){
              var field = data.field;
              if(field.status){
                field.status = 1;
              } else {
                field.status = 0;
              }
              $.ajax({
                url:"{:url('cases/categoryEdit')}",
                type:'post',
                data:{'field': field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    table.reload('LAY-app-content-list');
                    layer.close(index);
                  }
                  else{
                    return layer.msg(res.msg,{icon:5});
                  }
                }
              });
              form.render();
            });
            submit.trigger('click');
          }
        });
      }
    });

    var $ = layui.$, active = {
      insert: function(){
        layer.open({type: 2,
          title: '添加分类'
          , content: "{:url('cases/categoryInsert')}"
          , maxmin: true
          , area: ['400px', '400px']
          , btn: ['确定', '取消']
          , yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-submit");

            iframeWindow.layui.form.on('submit(Lay-app-form-submit)', function(data){
              var field = data.field;
              if(field.status){
                field.status = 1;
              } else {
                field.status = 0;
              }
              if(field.name == ''){ layer.msg('名称不能为空', {icon:5});}
              $.ajax({
                url : "{:url('cases/categoryInsert')}",
                type : 'post',
                data : {'field': field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    layer.close(index);
                    table.reload('LAY-app-content-list');
                  }
                  else{
                    return layer.msg(res.msg,{icon:5});
                  }
                }
              });

            });
            submit.trigger('click');
          }
        });
      }
      , article: function(){
        window.location.href = "{:url('cases/index')}";
      }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
</body>
</html>
